package com.sdtbu.ylgh.service.impl;

import com.sdtbu.ylgh.utils.JwtHelper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sdtbu.ylgh.dao.UserDao;
import com.sdtbu.ylgh.entity.UserEntity;
import com.sdtbu.ylgh.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    /**
     * 用户登录功能
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> login(Map<String, Object> params){
        List<UserEntity> list = listByMap(params);
        Map<String, Object> res = new HashMap<>();
        if (list.size() == 0){
            //用户或密码错误，走下面的 return map；
        }
        else if (list.size() == 1){
            UserEntity user = list.get(0);
            Map<String, Object> map = new HashMap<>();
            map.put("name", user.getName());
            map.put("nickName", user.getNickName());
            map.put("phone", user.getPhone());
            map.put("openid", user.getOpenid());
            map.put("id", user.getId());
            String token = JwtHelper.createToken(user.getId(), user.getNickName());
            res.put("user", map);
            res.put("token", token);
        }
        else{
            return null;
        }
        return res;
    }

    @Override
    public Map<String, Object> getinfo(String token) {
        Long id = JwtHelper.getUserId(token);
        System.out.println("id:" + id);
        if (id == null)
            return new HashMap<>();
        UserEntity user = getById(id);
        Map<String, Object> map = new HashMap<>();
        String name = user.getName();
        map.put("name", name);
        map.put("nickName", user.getNickName());
        map.put("phone", user.getPhone());
        map.put("openid", user.getOpenid());
        map.put("id", user.getId());
        return map;
    }

}